import md5 from 'js-md5';
function getUrlParam(search, name) {
  var reg = new RegExp('(\\?|#|&)' + name + '=(.*?)(&|#|$)', 'i');
  var r = search.substr(1).match(reg);
  if (r != null) return decodeURI(r[2]);
  return null;
}
const dateFormater = (formater, time) => {
  // console.log(new Date(time), new Date());
  let date = time ? new Date(time) : new Date(),
    Y = date.getFullYear() + '',
    M = date.getMonth() + 1,
    D = date.getDate(),
    H = date.getHours(),
    m = date.getMinutes(),
    s = date.getSeconds();
  return formater.replace(/YYYY|yyyy/g, Y)
    .replace(/YY|yy/g, Y.substr(2, 2))
    .replace(/MM/g, (M < 10 ? '0' : '') + M)
    .replace(/DD/g, (D < 10 ? '0' : '') + D)
    .replace(/HH|hh/g, (H < 10 ? '0' : '') + H)
    .replace(/mm/g, (m < 10 ? '0' : '') + m)
    .replace(/ss/g, (s < 10 ? '0' : '') + s)
}

// dateFormater('YYYY-MM-DD HH:mm:ss')

// dateFormater('YYYYMMDDHHmmss')

function getMD5Sign(signJson) {
  //signJson  参数json
  var keys = [];
  for (let k in signJson) {
    keys.push(k);
  }
  keys.sort();
  var kv = [];
  for (let k in keys) {
    kv.push(keys[k] + '' + signJson[keys[k]]);
  }
  var sign = kv.join('');
  sign = sign + 'AUnYDF4U7A0Q77Ns';
  sign=encodeURIComponent(sign)
  sign = md5(sign).toUpperCase();
  return sign;
}
function getMD5SignB(signJson) {
  //signJson  参数json
  var keys = [];
  for (let k in signJson) {
    keys.push(k);
  }
  keys.sort();
  var kv = [];
  for (let k in keys) {
    kv.push(keys[k] + '' + signJson[keys[k]]);
  }
  var sign = kv.join('');
  sign = sign + 'asd34fw1!@@12d';
  sign=encodeURIComponent(sign)
  sign = md5(sign).toUpperCase();
  return sign;
}

export {
  getUrlParam,
  dateFormater,
  getMD5Sign,
  getMD5SignB,
}